<?php

namespace Frontend\Controllers;

use Models\WxAreply;
use Models\WxImg;
use Models\WxImgmulti;
use Models\WxKeyword;
use Models\WxText;
use Models\Wx;
use \Wechat;

class WechatController extends BaseController
{
    public function beforeExecuteRoute($dispatcher)
    {
        parent::beforeExecuteRoute($dispatcher);
        include $this->config->application->libDir . "wechat.class.php";
    }

    public function indexAction($token)
    {
        $wx=Wx::findFirstBytoken($token);
        $config = $this->config;
        //TODO 优化参数获取方式，检查token安全性
//        $token = $this->request->get('token');
        $options = array(
            'token' => $token, //填写你设定的key
//            'encodingaeskey' => $wx->encodingaeskey, //填写加密用的EncodingAESKey
        );
        $wechat = new Wechat($options); //创建实例对象
        $wechat->valid(); //明文或兼容模式可以在接口验证通过后注释此句，但加密模式一定不能注释，否则会验证失败

        $type = $wechat->getRev()->getRevType();
        $openid=$wechat->getRev()->getRevFrom();
        switch ($type) {
            case Wechat::MSGTYPE_TEXT:
                $keyword = $wechat->getRev()->getRevContent();
                $type = WxKeyword::findFirst("keyword='" . $keyword . "'and token='" . $token . "'");
                if ($type->module == "Img") {
                    $img = WxImg::findFirst("keyword='" . $keyword . "'and token='" . $token . "'");
                    $url = strpos($img->url, "i-yizhan.com");
                    if ($img->url == "") {
                        $img->url = $config->host . '/frontend/wechat/imgtext/' . $img->id . '?token=' . $token;
                    }
                    if ($url) {
                        $img->url = $img->url . "&token=".$token."&openid=".$openid;
                    }
                    $news = array(
                        "0" => array(
                            'Title' => $img->title,
                            'Description' => $img->intro,
                            'PicUrl' => $img->pic,
                            'Url' => $img->url
                        ),
                    );
                    $wechat->news($news)->reply();
                } elseif ($type->module == "Text") {
                    $text = WxText::findFirst("keyword='" . $keyword . "'and token='" . $token . "'");
                    $wechat->text($text->text)->reply();
                } elseif ($type->module == "Multi") {
                    $imgs = WxImgmulti::findFirst("keyword='" . $keyword . "'and token='" . $token . "'");
                    $id = explode(',', $imgs->imgs);
                    foreach ($id as $k => $v) {
                        $img = WxImg::findFirstByid($v);
                        $url = strpos($img->url, "i-yizhan.com");
                        if ($img->url == "") {
                            $img->url = $config->host . '/frontend/wechat/imgtext/' . $img->id . '?token=' . $token;
                        }
                        if ($url) {
                            $img->url = $img->url . "&token=".$token."&openid=".$openid;
                        }
                        $news[$k] = array(
                            'Title' => $img->title,
                            'Description' => $img->intro,
                            'PicUrl' =>$img->pic,
                            'Url' => $img->url
                        );
                    }
                    $wechat->news($news)->reply();

                }
                break;
            case Wechat::MSGTYPE_EVENT:
                $keyword = $wechat->getRev()->getRevEvent();
                if ($keyword['event'] == "subscribe") {
                    $text = WxAreply::findFirst("token='" . $token . "'");
                    if ($text->check == "on") {
                        $type = WxKeyword::findFirst("keyword='" . $text->keyword . "'and token='" . $token . "'");
                        if ($type->module == "Img") {
                            $img = WxImg::findFirst("id='" . $type->pid . "'");
                            $url = strpos($img->url, "i-yizhan.com");
                            if ($img->url == "") {
                                $img->url = $config->host . '/frontend/wechat/imgtext/' . $img->id . '?token=' . $token;
                            }
                            if ($url) {
                                $img->url = $img->url . "&token=".$token."&openid=".$openid;
                            }
                            $news = array(
                                "0" => array(
                                    'Title' => $img->title,
                                    'Description' => $img->intro,
                                    'PicUrl' => $img->pic,
                                    'Url' => $img->url
                                ),
                            );
                            $wechat->news($news)->reply();
                        } elseif ($type->module == "Text") {
                            $text = WxText::findFirst("id='" . $type->pid . "'");
                            $wechat->text($text->text)->reply();
                        } elseif ($type->module == "Multi") {
                            $imgs = WxImgmulti::findFirst("id='" . $type->pid . "'");
                            $id = explode(',', $imgs->imgs);
                            foreach ($id as $k => $v) {
                                $img = WxImg::findFirstByid($v);
                                $url = strpos($img->url, "i-yizhan.com");
                                if ($img->url == "") {
                                    $img->url = $config->host . '/frontend/wechat/imgtext/' . $img->id . '?token=' . $token;
                                }
                                if ($url) {
                                    $img->url = $img->url . "&token=".$token."&openid=".$openid;
                                }
                                $news[$k] = array(
                                    'Title' => $img->title,
                                    'Description' => $img->intro,
                                    'PicUrl' => $img->pic,
                                    'Url' => $img->url
                                );
                            }
                            $wechat->news($news)->reply();

                        }
                    } else
                        $wechat->text($text->content)->reply();
                }
                if ($keyword['event'] == "CLICK") {
                    $key = $keyword['key'];
                    $type = WxKeyword::findFirst("keyword='" . $key . "'and token='" . $token . "'");
                    if ($type->module == "Img") {
                        $img = WxImg::findFirst("keyword='" . $key . "'and token='" . $token . "'");
                        $url = strpos($img->url, "i-yizhan.com");
                        if ($img->url == "") {
                            $img->url = $config->host . '/frontend/wechat/imgtext/' . $img->id . '?token=' . $token;
                        }
                        if ($url) {
                            $img->url = $img->url . "&token=".$token."&openid=".$openid;
                        }

                        $news = array(
                            "0" => array(
                                'Title' => $img->title,
                                'Description' => $img->intro,
                                'PicUrl' => $img->pic,
                                'Url' => $img->url
                            ),
                        );
                        $wechat->news($news)->reply();
                    } elseif ($type->module == "Text") {
                        $text = WxText::findFirst("keyword='" . $key . "'and token='" . $token . "'");
                        $wechat->text($text->text)->reply();
                    } elseif ($type->module == "Multi") {
                        $imgs = WxImgmulti::findFirst("keyword='" . $key . "'and token='" . $token . "'");
                        $id = explode(',', $imgs->imgs);
                        foreach ($id as $k => $v) {
                            $img = WxImg::findFirstByid($v);
                            $url = strpos($img->url, "i-yizhan.com");
                            if ($img->url == "") {
                                $img->url = $config->host . '/frontend/wechat/imgtext/' . $img->id . '?token=' . $token;
                            }
                            if ($url) {
                                $img->url = $img->url . "&token=".$token."&openid=".$openid;
                            }
                            $news[$k] = array(
                                'Title' => $img->title,
                                'Description' => $img->intro,
                                'PicUrl' => $img->pic,
                                'Url' => $img->url
                            );
                        }
                        $wechat->news($news)->reply();
                    }
                }
                break;
            case Wechat::MSGTYPE_IMAGE:
                //TODO
                break;
            default:
                //TODO 实现默认回复逻辑
                $wechat->text("help info")->reply();
        }
    }

    public function imgtextAction($id)
    {
        //TODO 根据token做安全检查
        $token = $this->request->get('token');
        $it = WxImg::findFirst($id);
        $it->click+=1;
        $it->update();
        $this->view->it = $it;
    }

}
